var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { generateDecoratedMintNavigationFromPages, getConfigObj, categorizeFiles, createPage, } from '@mintlify/prebuild';
import { promises as _promises } from 'fs';
import { outputFile } from 'fs-extra';
import { join } from 'path';
import { CMD_EXEC_PATH } from '../../constants.js';
const { readFile } = _promises;
const createFilenamePageMetadataMap = (contentDirectoryPath, contentFilenames, openApiFiles, clientPath, writeFiles = false) => __awaiter(void 0, void 0, void 0, function* () {
    let pagesAcc = {};
    const contentPromises = [];
    contentFilenames.forEach((filename) => {
        contentPromises.push((() => __awaiter(void 0, void 0, void 0, function* () {
            const sourcePath = join(contentDirectoryPath, filename);
            const contentStr = (yield readFile(sourcePath)).toString();
            const { slug, pageMetadata, pageContent } = yield createPage(filename, contentStr, contentDirectoryPath, openApiFiles, true);
            if (clientPath && writeFiles) {
                const targetPath = join(clientPath, 'src', '_props', filename);
                yield outputFile(targetPath, pageContent, {
                    flag: 'w',
                });
            }
            pagesAcc = Object.assign(Object.assign({}, pagesAcc), { [slug]: pageMetadata });
        }))());
    });
    yield Promise.all(contentPromises);
    return pagesAcc;
});
export const generateNav = () => __awaiter(void 0, void 0, void 0, function* () {
    const { contentFilenames, openApiFiles } = yield categorizeFiles(CMD_EXEC_PATH);
    const [filenamePageMetadataMap, configObj] = yield Promise.all([
        createFilenamePageMetadataMap(CMD_EXEC_PATH, contentFilenames, openApiFiles),
        getConfigObj(CMD_EXEC_PATH),
    ]);
    return generateDecoratedMintNavigationFromPages(filenamePageMetadataMap, configObj === null || configObj === void 0 ? void 0 : configObj.navigation);
});
